1382B - Sequential Nim - CodeForces Solution


dp games *1100

Please click on ads to support us..

Python Code:

from collections import deque
from collections import OrderedDict
import queue
import heapq
import math
import sys

sys.setrecursionlimit(10**6)
 
def sf(): return [int(x) for x in input().split(" ")]
def sfi(): return int(input())
def sfs(): return input()
def printf(x):
    print(x)
    sys.stdout.flush()



def main():
    t = int(input())
    for tc in range(t):
        n = int(input())
        piles = [int(x) for x in input().split(" ")]

        winner = -1
        ct = 0
        while ct < n and piles[ct] == 1:
            winner = winner != 1
            ct+=1
        
        if ct == n: 
            winner = winner != 1

        if winner <= 0: print("First")
        else: print("Second")

if __name__ == "__main__":
    main()

C++ Code:

#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long i64;

const i64 mod = 1e9 + 7;

void solve(){
	int n;
	cin >> n;
	vector<int> a(n);
	for(int &x: a) cin >> x;
	if(n == 1) {
		cout << "First\n";
	}
	else {
		int curr = 0;
		for(int i = n - 2; i >= 0; i--)
		{
			if(curr == 0)
			{
				if(a[i] == 1)
				{
					curr = 1;
				}
				else 
				{
					curr = 0;
				}
			}
			else 
			{
				if(a[i] == 1) 
				{
					curr = 0;
				}
				else 
				{
					curr = 0;
				}
			}
		}
		if(curr == 1) 
		{
			cout << "Second\n";
		}
		else 
		{
			cout << "First\n";
		}
	}
}
int main(){	
	ios::sync_with_stdio(0);
	cin.tie(0);
	int tc;
	cin >> tc;
	while(tc--) solve();
	return 0;
}   
   


Comments

Submit
0 Comments
More Questions

1535A - Fair Playoff
1538F - Interesting Function
1920. Build Array from Permutation
494. Target Sum
797. All Paths From Source to Target
1547B - Alphabetical Strings
1550A - Find The Array
118B - Present from Lena
27A - Next Test
785. Is Graph Bipartite
90. Subsets II
1560A - Dislike of Threes
36. Valid Sudoku
557. Reverse Words in a String III
566. Reshape the Matrix
167. Two Sum II - Input array is sorted
387. First Unique Character in a String
383. Ransom Note
242. Valid Anagram
141. Linked List Cycle
21. Merge Two Sorted Lists
203. Remove Linked List Elements
733. Flood Fill
206. Reverse Linked List
83. Remove Duplicates from Sorted List
116. Populating Next Right Pointers in Each Node
145. Binary Tree Postorder Traversal
94. Binary Tree Inorder Traversal
101. Symmetric Tree
77. Combinations